package model;

import databaseHandling.SalgsHandler;
import interfaces.SalgInterface;
import interfaces.VareInterface;
import interfaces.VareSalgInterface;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Maja
 */
public class Salg extends UnicastRemoteObject implements SalgInterface {

    private int id;
    private int totalpris;
    private String salgsDato;
    private int status;
    private Bruger bruger;
    private ArrayList<VareSalgInterface> vareSalgsListe;

    public Salg(int id, int totalpris, String salgsDato, int status, Bruger bruger) throws RemoteException {
        this.id = id;
        this.totalpris = totalpris;
        this.salgsDato = salgsDato;
        this.status = status;
        this.bruger = bruger;
        vareSalgsListe = new ArrayList<>();
    }

    @Override
    public int getId() throws RemoteException {
        return id;
    }

    @Override
    public int getTotalPris() throws RemoteException {
        return totalpris;
    }

    @Override
    public String getSalgDato() throws RemoteException {
        return salgsDato;
    }

    @Override
    public int getStatus() throws RemoteException {
        return status;
    }

    @Override
    public Bruger getBruger() throws RemoteException {
        return bruger;
    }

    @Override
    public ArrayList<VareSalgInterface> getVareSalgsListe() throws RemoteException {
        return (ArrayList<VareSalgInterface>) vareSalgsListe.clone();
    }

    public void setVareSalgsListe(ArrayList<VareSalgInterface> vareSalgsListe) {
        this.vareSalgsListe = vareSalgsListe;
    }

    @Override
    public void addVareSalg(VareInterface vare, int antal) throws RemoteException, SQLException {
        vareSalgsListe.add((VareSalgInterface)new VareSalg(antal, vare.getSalgsPris(), vare));
        SalgsHandler.tilføjVareTilSalg(vare.getId(), id, antal, vare.getSalgsPris()*antal);
        
        int pris = 0;
        for (int i = 0; i < vareSalgsListe.size(); i++) {
            pris = pris + vareSalgsListe.get(i).getSalgsPris()*antal;
        }
        totalpris = pris;
        System.out.println(totalpris);   
        GourmetGrossist.getInstance().notifyListeners(new RemoteAction("addVareSalg", vare, 0));
    }

}
